﻿using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.Repository;
using Net5CommonLib.SystemLib.TypeTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VueAndNetCoreEfApi.IRepository.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.departmentyrh;

namespace VueAndNetCoreEfApi.Repository.SystemAdmin
{
    public class departmentRespository : BaseRepository, IdepartmentRespository
    {
        /// <summary>
        /// 部门管理显示分页
        /// </summary>
        /// <param name="sm"></param>
        /// <returns></returns>
        public PagerResult<department> GetdepartmentList(SearchModel sm)
        {
            IQueryable<department> iq = SoaSystemCenterDbContext.department.AsQueryable();
            if (sm.SearchItem != null && sm.SearchItem.Count > 0)
            {
                var tempDi = sm.SearchItem;
                string dp_name = "dp_name";
                if (tempDi.Keys.Contains(dp_name) && !string.IsNullOrEmpty(tempDi[dp_name]))
                {
                    iq = iq.Where(c => c.dp_name.Contains(tempDi[dp_name]));
                }
            }
            return Pager(iq, sm);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool departmentAdd(department user)
        {
            SoaSystemCenterDbContext.Add(user);
            SoaSystemCenterDbContext.SaveChanges();
            return true;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public bool departmentDelete(int id)
        {
            var dbUser = SoaSystemCenterDbContext.department.FirstOrDefault(c => c.dp_id == id);
            if (dbUser == null)
            {
                return false;
            }
            string delsql = "delete from department where dp_id=@p0";
            SoaSystemCenterDbContext.Database.ExecuteSqlRaw(delsql, dbUser.dp_id);
            SoaSystemCenterDbContext.SaveChanges();
            return false;
        }

        public department departmentFantian(int id)
        { 
           department userPo = SoaSystemCenterDbContext.department.FirstOrDefault(c => c.dp_id == id);
            return userPo;

        }
        public bool departmentUpdate(department user)
        {
            if (user == null) return false;
            SoaSystemCenterDbContext.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            SoaSystemCenterDbContext.SaveChanges();
            return true;
        }
       
        
    }
}
